﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace UpdaterForm
{//huinja udalit
    [Serializable]
    public class AppData
    {
        private String _logFilePath;
        [XmlElement("Name")]
        public String AppName { get; set; }
        [XmlElement("CurrentVersion")]
        public String CurrentVersion { get; set; }
        [XmlElement("InstallationPath")]
        public String InstallationPath { get; set; }
        [XmlElement("UpdateLogFilePath")]
        public String UpdateLogFilePath {
            get
            {
                if (String.IsNullOrEmpty(_logFilePath)){
                    _logFilePath = AppName + "Log.xml";
                }
                return _logFilePath;
            }
            set { _logFilePath = value; } 
        }

        
        [XmlIgnore]
        private History _history;
        [XmlIgnore]
        public History History {
            get {
                if (_history==null)
                {
                    LoadLogs();
                }
                return _history;
            } 
        }

        private void LoadLogs()
        {
            
            var serializer = new XmlSerializer(typeof(History));

            if(!File.Exists(UpdateLogFilePath)){
                _history = new History {Logs = new List<UpdateLog>()};
                SaveLogs();
                return;
            }

            using (var reader = new StreamReader(UpdateLogFilePath)){
               _history =  (History)serializer.Deserialize(reader);
            }
            if (_history == null)
            {
                _history = new History();
            }
            
        }

        public  void SaveLogs(){
           var serializer = new XmlSerializer(typeof(History));
               using (var writer = new StreamWriter(UpdateLogFilePath))
               {
                   serializer.Serialize(writer, _history);
               }
           
       }
    }
}
